var https = require('https');
var fs = require('fs');
var cheerio = require('cheerio');
var request = require('request');
var express = require('express');
var router = express.Router();
var topArticles = [];//{name:'书名',author:'作者',rank:'排名',code:'图书编号',image:'图片',rating:'评分'}
//爬取豆瓣图书排行的一页数据
router.get("/",function (req, res) {
 function startRequest(url,page){
    https.get(url,function(res){
        var html = '';
        res.setEncoding('utf-8');
        res.on('data',function(chunk){
            html+=chunk;
        });
        res.on('end',function(){
            let index = 1;
            var $ = cheerio.load(html);
            var $books = $('#content .article table');
            console.log('获取第'+page+'页数据成功,数量'+$books.length);
            $books.each(function(){
                var article = {
                    name:$(this).find('.pl2 a').attr('title'),
                    author:$(this).find('.pl').text().split('/',1)[0],
                    code:$(this).find('.pl2 a').attr('href').match(/\d{5,}/)[0],
                    rank:(page-1)*25+index,
                    image:$(this).find('img').attr('src'),
                    rating:$(this).find('.rating_nums').text()
                };
                topArticles.push(article);
                index++;
            });
            console.log('第'+page+'页数据处理结束');
            if(topArticles.length>=250){
                saveDataToFile();
            }
        });
    }).on('error',function(err){
        console.log(err);
    });


}

function saveDataToFile(){
    topArticles.sort(function(a,b){//排序
        return a.rank-b.rank;
    });
    var str = JSON.stringify(topArticles).replace(/},{/g,'},\n{');
    console.log('准备写入,总数据：'+topArticles.length);
    console.log(str);
    res.render('./pachong/pachong',{title: '豆瓣评分', datas: topArticles});
   /* fs.writeFile('./pachong.txt', str,  function(err) {
        if (err) {
            return console.error(err);
        }
        console.log("数据写入成功！");
    });*/
}

for(let i=0;i<250;i=i+25){
    var url = 'https://book.douban.com/top250?start='+i;
    startRequest(url,i/25+1);
}
});





module.exports = router;
